//  体重控制
import MeasureItem from './MeasureItem'
import Calc from './Calc'

const UI = require('../style/UI');

var isAdd = false;
export default class WeightControlReport {
    /**
     * 体重控制
     * float[] values = new float[4];//脂肪重量,体重控制，脂肪控制，肌肉控制
     * String[] strings = new String[4];
     * <p>
     * // 体重 > 标准体重； 体脂率 > 标准脂肪
     * if (userWeight > standWeight && userBodyFat > standFat + 1) {
     * values[0] = NumberUtils.getOnePrecision(userWeight * userBodyFat / 100);
     * values[2] = NumberUtils.getOnePrecision((userBodyFat - standFat) / 100 * userWeight);
     * values[3] = NumberUtils.getOnePrecision(values[2] * 1 / 3);
     * values[1] = NumberUtils.getOnePrecision(values[2] - values[3]);
     */
    static getStand(measure) {
        let weightControl;
        if (measure.gender == 0) {
            weightControl = ((measure.height * 1.37) - 110) * 0.45;
        } else {
            weightControl = (measure.height - 80) * 0.7;
        }

        return weightControl;
    }

    static calcFfm(measure) {

        let weightControl;
        let standWeight = WeightControlReport.getStand(measure);
        let userWeight = measure.weight;
        let userBodyFat = measure.bodyfat;
        let d = 1;

        let standFat = WeightControlReport.getStandFat(measure);

        if (userWeight > standWeight && userBodyFat > standFat + 1) {
            weightControl = (((userBodyFat - standFat) / 100 * userWeight) * 2 / 3).toFixed(2);
            isAdd = false;
        } else if (userWeight < standWeight - d && userBodyFat < standFat) {
            isAdd = true;
            weightControl = ((standWeight - userWeight)) //增加
        } else { //无需控制
            weightControl = 0;
        }
        return weightControl;
    }

    static getStandFat(measure) {
        //在深度评估报告中最理想脂肪修改成：男性15%，⼥女性22%
        let standFat;
        if (measure.gender == 1) {
            standFat = 15;
        } else
            standFat = 22;
        return standFat;

    }

    static isEnable(measure) {
        return true
    }

    static build(measure) {

        let value = WeightControlReport.calcFfm(measure);
        let desc = '';
        const unit = "kg";
        const item = new MeasureItem();
        const standBmr = WeightControlReport.calcFfm(measure);
        const boundaries = [standBmr];
        let level = Calc.calcLevel(value, boundaries, 1);
        let isStand = level == 1;

        value = parseFloat((+value).toFixed(2));
        /* if (!value || value == 0) {
            return null;
        } */

        if (isAdd) {
            desc = "根据健康的体重控制方式，建议您当前需要增加" + value + unit + "体重";
        } else {
            desc = "根据健康的体重控制方式，建议您当前需要减少" + value + unit + "体重";
        }

        if (value == 0) {
            desc = "根据健康的体重控制方式，您维持当前体重控制即可。";
        }
        if (value != 0) {
            level = 0;
            isStand = false;

        } else {
            level = 1;
            isStand = true;
        }

        item.key = 'weightControl'
        item.name = "体重控制";
        item.value = value;
        item.isStand = isStand;
        item.unit = unit;
        item.levelNames = ['不达标', '标准'];
        item.bar = [UI.color.report_lower, UI.color.report_stand];
        item.showBar = false;
        item.desc = desc;
        item.level = level;

        return item
    }

}
